﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.MountAndBlade.View.ItemObjectViewExtensions
// Assembly: TaleWorlds.MountAndBlade.View, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 61152EB8-513B-4D4A-8AC0-0D1465C6CF6C
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\Modules\Native\bin\Win64_Shipping_Client\TaleWorlds.MountAndBlade.View.dll

using TaleWorlds.Core;
using TaleWorlds.DotNet;
using TaleWorlds.Engine;
using TaleWorlds.Library;

#nullable disable
namespace TaleWorlds.MountAndBlade.View
{
  public static class ItemObjectViewExtensions
  {
    public static MetaMesh GetCraftedMultiMesh(this ItemObject itemObject, bool needBatchedVersion)
    {
      CraftedDataView craftedDataView = CraftedDataViewManager.GetCraftedDataView(itemObject.WeaponDesign);
      return !needBatchedVersion ? craftedDataView?.NonBatchedWeaponMesh.CreateCopy() : craftedDataView?.WeaponMesh.CreateCopy();
    }

    public static MetaMesh GetMultiMeshCopy(this ItemObject itemObject)
    {
      MetaMesh craftedMultiMesh = itemObject.GetCraftedMultiMesh(true);
      if ((NativeObject) craftedMultiMesh != (NativeObject) null)
        return craftedMultiMesh;
      return string.IsNullOrEmpty(itemObject.MultiMeshName) ? (MetaMesh) null : MetaMesh.GetCopy(itemObject.MultiMeshName);
    }

    public static MetaMesh GetMultiMeshCopyWithGenderData(
      this ItemObject itemObject,
      bool isFemale,
      bool hasGloves,
      bool needBatchedVersion)
    {
      MetaMesh craftedMultiMesh = itemObject.GetCraftedMultiMesh(needBatchedVersion);
      if ((NativeObject) craftedMultiMesh != (NativeObject) null)
        return craftedMultiMesh;
      if (string.IsNullOrEmpty(itemObject.MultiMeshName))
        return (MetaMesh) null;
      MetaMesh copy1 = MetaMesh.GetCopy(isFemale ? itemObject.MultiMeshName + "_female" : itemObject.MultiMeshName + "_male", false, true);
      if ((NativeObject) copy1 != (NativeObject) null)
        return copy1;
      string multiMeshName = itemObject.MultiMeshName;
      MetaMesh copy2 = MetaMesh.GetCopy(!isFemale ? multiMeshName + (hasGloves ? "_slim" : "") : multiMeshName + (hasGloves ? "_converted_slim" : "_converted"), false, true);
      if ((NativeObject) copy2 != (NativeObject) null)
        return copy2;
      MetaMesh copy3 = MetaMesh.GetCopy(itemObject.MultiMeshName, mayReturnNull: true);
      return (NativeObject) copy3 != (NativeObject) null ? copy3 : (MetaMesh) null;
    }

    public static MatrixFrame GetScaledFrame(
      this ItemObject itemObject,
      Mat3 rotationMatrix,
      MetaMesh metaMesh,
      float scaleFactor,
      Vec3 positionShift)
    {
      MatrixFrame identity = MatrixFrame.Identity;
      Vec3 v1_1 = new Vec3(1000000f, 1000000f, 1000000f);
      Vec3 v1_2 = new Vec3(-1000000f, -1000000f, -1000000f);
      for (int meshIndex = 0; meshIndex != metaMesh.MeshCount; ++meshIndex)
      {
        Vec3 boundingBoxMin = metaMesh.GetMeshAtIndex(meshIndex).GetBoundingBoxMin();
        Vec3 boundingBoxMax = metaMesh.GetMeshAtIndex(meshIndex).GetBoundingBoxMax();
        Vec3[] vec3Array = new Vec3[8]
        {
          rotationMatrix.TransformToParent(new Vec3(boundingBoxMin.x, boundingBoxMin.y, boundingBoxMin.z)),
          rotationMatrix.TransformToParent(new Vec3(boundingBoxMin.x, boundingBoxMin.y, boundingBoxMax.z)),
          rotationMatrix.TransformToParent(new Vec3(boundingBoxMin.x, boundingBoxMax.y, boundingBoxMin.z)),
          rotationMatrix.TransformToParent(new Vec3(boundingBoxMin.x, boundingBoxMax.y, boundingBoxMax.z)),
          rotationMatrix.TransformToParent(new Vec3(boundingBoxMax.x, boundingBoxMin.y, boundingBoxMin.z)),
          rotationMatrix.TransformToParent(new Vec3(boundingBoxMax.x, boundingBoxMin.y, boundingBoxMax.z)),
          rotationMatrix.TransformToParent(new Vec3(boundingBoxMax.x, boundingBoxMax.y, boundingBoxMin.z)),
          rotationMatrix.TransformToParent(new Vec3(boundingBoxMax.x, boundingBoxMax.y, boundingBoxMax.z))
        };
        for (int index = 0; index < 8; ++index)
        {
          v1_1 = Vec3.Vec3Min(v1_1, vec3Array[index]);
          v1_2 = Vec3.Vec3Max(v1_2, vec3Array[index]);
        }
      }
      float num1 = 1f;
      if (itemObject.PrimaryWeapon != null && itemObject.PrimaryWeapon.IsMeleeWeapon)
        num1 = MBMath.ClampFloat((float) (0.30000001192092896 + (double) itemObject.WeaponComponent.PrimaryWeapon.WeaponLength / 1.6000000238418579), 0.5f, 1f);
      Vec3 vec3 = (v1_1 + v1_2) * 0.5f;
      float num2 = MathF.Max(v1_2.x - v1_1.x, v1_2.y - v1_1.y);
      float scaleAmount = scaleFactor * num1 / num2;
      identity.origin -= vec3 * scaleAmount;
      identity.origin += positionShift;
      identity.rotation = rotationMatrix;
      identity.rotation.ApplyScaleLocal(scaleAmount);
      return identity;
    }
  }
}
